package com.dlwx.parttimetalent.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.dlwx.parttimetalent.R;
import com.dlwx.parttimetalent.adapter.OrderPublishAdapter;
import com.dlwx.parttimetalent.base.BaseActivity;
import com.dlwx.parttimetalent.bean.MyPublishBean;
import com.dlwx.parttimetalent.comm.Constant;
import com.dlwx.parttimetalent.comm.Http;
import com.dlwx.parttimetalent.utils.DealResult;
import com.dlwx.parttimetalent.utils.OkgoUtil;
import com.dlwx.parttimetalent.utils.SPUtils;
import com.dlwx.parttimetalent.utils.ToastUtils;
import com.dlwx.parttimetalent.views.ShareDialog;
import com.github.jdsjlzx.ItemDecoration.DividerDecoration;
import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PublishActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.title_right_one_tv)
    TextView titleRightOneTv;
    @BindView(R.id.recyclerView)
    LRecyclerView recyclerView;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    @BindView(R.id.title_right_one_tv2)
    TextView titleRightOneTv2;
    private OrderPublishAdapter mAdapter;
    private int page = 1;
    private String longi;
    private String lat;
    private String cityStr;
    private LRecyclerViewAdapter lRecyclerViewAdapter;
    private String qrcode, shareurl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_publish);
        ButterKnife.bind(this);
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        refresh();
    }

    private void getDatas() {
        loadingDialog.show();
        OkgoUtil.post(Http.ReleaseList, this)
                .params("pageno", page)
                .params("long", longi)
                .params("lat", lat)
                .params("city", cityStr)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDialog.dismiss();
                        MyPublishBean parentBean = DealResult.getInstance().dealDatas(PublishActivity.this, response, new TypeToken<MyPublishBean>() {
                        }.getType());
                        if (parentBean == null) {
                            return;
                        }
                        qrcode=parentBean.getQrcode();
                        shareurl=parentBean.getShareurl();
                        if (parentBean.getCode() == Constant.SUCESS || parentBean.getCode() == Constant.EMPTY) {
                            mAdapter.addAll(parentBean.getBody());
                            recyclerView.refreshComplete(mAdapter.getItemCount());
                        } else {
                            ToastUtils.toastForShort(PublishActivity.this, parentBean.getResult());
                        }

                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDialog.dismiss();
                        recyclerView.refreshComplete(mAdapter.getItemCount());
                        ToastUtils.toastForShort(PublishActivity.this, getString(R.string.bad_net));
                    }
                });
    }

    private void initView() {
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        tvMiddleTitle.setVisibility(View.VISIBLE);
        titleRightOneTv.setVisibility(View.VISIBLE);
        titleRightOneTv2.setVisibility(View.VISIBLE);
        tvMiddleTitle.setText("发布");
        titleRightOneTv.setText("");
        titleRightOneTv.setBackground(ContextCompat.getDrawable(this,R.mipmap.more));
        titleRightOneTv2.setText("我的发布");
        titleRightOneTv2.getPaint().setTextSize(26);
        longi = (String) SPUtils.get(this, Constant.LOCATION_LON, "116.395645");
        lat = (String) SPUtils.get(this, Constant.LOCATION_LAT, "39.929985");
        cityStr = (String) SPUtils.get(this, Constant.CITY, "");
        recyclerView.setLayoutManager(new LinearLayoutManager(PublishActivity.this));
        mAdapter = new OrderPublishAdapter(PublishActivity.this);
        mAdapter.setDataList(new ArrayList<MyPublishBean.BodyBean>());
        lRecyclerViewAdapter = new LRecyclerViewAdapter(mAdapter);
        //divider
        DividerDecoration divider = new DividerDecoration.Builder(PublishActivity.this)
                .setHeight(R.dimen.size_2)
//                .setPadding(R.dimen.divider_padding)
                .setColorResource(R.color.white)
                .build();
        recyclerView.setHasFixedSize(true);
        recyclerView.addItemDecoration(divider);
        recyclerView.setAdapter(lRecyclerViewAdapter);
        recyclerView.setLoadMoreEnabled(true);
        recyclerView.setPullRefreshEnabled(true);
        recyclerView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });
        recyclerView.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                page++;
                getDatas();
            }
        });
        lRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent i = new Intent(PublishActivity.this, PublishDetailActivity.class);
                i.putExtra(Constant.IS_ME, false);
                i.putExtra("id", mAdapter.getDataList().get(position).getWorkid() + "");
                startActivity(i);
            }
        });
    }

    private void refresh() {
        page = 1;
        mAdapter.clear();
        lRecyclerViewAdapter.notifyDataSetChanged();
        getDatas();
    }

    @OnClick({R.id.title_left_one_btn, R.id.title_right_one_tv, R.id.tv_confirm,R.id.title_right_one_tv2})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_one_btn:
                this.finish();
                break;
            case R.id.title_right_one_tv:
                doShare();
                break;
            case R.id.tv_confirm:
                startActivity(new Intent(this, PublishInfoActivity.class));
                break;
            case R.id.title_right_one_tv2:
                startActivity(new Intent(this, MyPublishActivity.class));
                break;
        }
    }

    private void doShare() {
        View contentView = LayoutInflater.from(PublishActivity.this).inflate(R.layout.popup_more, null);
        TextView tvRemark = contentView.findViewById(R.id.tv_remark);
        TextView tvMoney = contentView.findViewById(R.id.tv_money);
        TextView tvShare = contentView.findViewById(R.id.tv_share);
        TextView tvAttention = contentView.findViewById(R.id.tv_attention);
        tvRemark.setVisibility(View.GONE);
        tvAttention.setVisibility(View.GONE);
        final PopupWindow popWnd = new PopupWindow(PublishActivity.this);
        popWnd.setContentView(contentView);
        popWnd.setBackgroundDrawable(null);
        popWnd.setWidth(380);
        popWnd.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        popWnd.setOutsideTouchable(true);
        if (!popWnd.isShowing()) {
            popWnd.showAsDropDown(titleRightOneTv2, -360, 20);
        }
        tvMoney.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popWnd.dismiss();
                ShareDialog dialog = new ShareDialog(PublishActivity.this, shareurl);
                dialog.show();
            }
        });
        tvShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popWnd.dismiss();
                ShareDialog dialog = new ShareDialog(PublishActivity.this, qrcode);
                dialog.show();

            }
        });
    }
}
